//
//  MHBindViewModel.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/6/26.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHBindViewModel.h"
//#import ""

@implementation MHBindViewModel
-(void)initialize
{
    [super initialize];
    
    self.title = self.isModify ? @"修改手机号":@"绑定手机号";
    @weakify(self);
    self.bindPhoneCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input){
        @strongify(self);
        
        if(self.isModify)
        {
            // 修改第一步;
            MHBindViewModel *bindViewModel = [[MHBindViewModel alloc] initWithServices:self.services params:@{@"isModifyFinished":@(true)}];
            bindViewModel.oldVerifyCode = self.verifyCode;
            [self.services pushViewModel:bindViewModel animated:YES];
            return [RACSignal empty];
            
        }else if(self.isModifyFinished){
            
            //第二部
            return [[self.services.client  changeBindTelephone:self.telephone newVerifyCode:self.verifyCode oldVerifyCode:self.oldVerifyCode] doNext:^(id x) {
                [self.services popViewModelAnimated:YES];
            }];
        }else
        {
            return [[self.services.client  bindTelephoneWithTelephone:self.telephone  verifyCode:self.verifyCode] doNext:^(id x) {
                [self.services popViewModelAnimated:YES];
            }];
        }
       
    }];

    self.sendVerifyCodeCommand  = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input){
        @strongify(self);
        return [self.services.client  createVerifyCode:self.telephone];
    }];
    
    
    
}

- (instancetype)initWithServices:(id<MHViewModelServices>)services params:(NSDictionary *)params{
    if (self = [super initWithServices:services params:params]) {
        self.isModify   = [params[MHViewModelUtilKey] boolValue];
        self.isModifyFinished   = [params[@"isModifyFinished"] boolValue];
    }
    return self;
}

@end
